﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Globalization;
/// <summary>
///WriteError 的摘要说明
/// </summary>
public class WriteError
{
    public WriteError()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static void Write(string errorMessage)
    {
        try
        {
            string path = "~/Error/" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
            if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
            }
            using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
            {
                w.WriteLine("\r\n日志条目 ");
                w.WriteLine("发生时间:{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
                string err = "错误位置:" + System.Web.HttpContext.Current.Request.Url.ToString() + ".\r\n错误信息:" + errorMessage;
                w.WriteLine(err);
                w.WriteLine("来访IP:" + System.Web.HttpContext.Current.Request.UserHostAddress);
                w.WriteLine("__________________________");
                w.Flush();
                w.Close();
            }
        }
        catch (Exception ex) 
        {
            Write(ex.Message);
        }
    }
}
